#!/usr/bin/node

const pic = process.argv[2];

if(process.argv.length !== 3) {
  console.error('命令行格式应该是cmd filename');
  process.exit(1);
}
const fs = require('fs');

try {
  var data = fs.readFileSync(pic);
  var url = data.toString('base64');
} catch(e) {
  console.error(e.message);
  process.exit(2);
}


//console.log(url);

let html = `<html><body>
            <img src="data:image/jpg;base64,${url}" /></body></html>`;

const http = require('http');
http.createServer((req, res) => {
  console.log(req.headers);
  console.log(req.url);
  res.end(html);
}).listen(8080);

//运行node 04-data-uri.js qr-code.jpg
//浏览器打开192.168.145.144:8080就行了
